package com.wondersgroup.insurance.dnuilearn.recyclerview.swipe;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wondersgroup.insurance.dnuilearn.R;

import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2017/6/28.
 */

public class QQMessageAdapter extends RecyclerView.Adapter<QQMessageAdapter.MyViewHolder> implements  ItemTouchHelperAdapterCallback{
    private List<QQMessage> list;

    public QQMessageAdapter(List<QQMessage> list) {
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext().getApplicationContext()).inflate(R.layout.listitem,parent,false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder,final int position) {
       final QQMessage qqMessage = list.get(position);
        holder.iv_logo.setImageResource(qqMessage.getLogo());
        holder.tv_name.setText(qqMessage.getName());
        holder.tv_lastMsg.setText(qqMessage.getLastMsg());
        holder.tv_time.setText(qqMessage.getTime());
        holder.iv_logo.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // 传递事件
                if(event.getAction()==MotionEvent.ACTION_DOWN){
//					ItemTouchHelper.startDrag();
//
                    Log.e(""," position = "+position + "  qqMessage = "+qqMessage.getName());
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        Collections.swap(list,fromPosition,toPosition);
        notifyItemMoved(fromPosition,toPosition);
    }

    @Override
    public void onItemSwipe(int position) {
        list.remove(position);
        notifyItemRemoved(position);
    }

    static class MyViewHolder extends RecyclerView.ViewHolder{
       public ImageView iv_logo;
       public TextView tv_name;
       public TextView tv_time;
       public TextView tv_lastMsg;
       public MyViewHolder(View itemView) {
           super(itemView);
           iv_logo = (ImageView) itemView.findViewById(R.id.iv_logo);
           tv_name = (TextView)itemView.findViewById(R.id.tv_name);
           tv_time = (TextView)itemView.findViewById(R.id.tv_time);
           tv_lastMsg = (TextView)itemView.findViewById(R.id.tv_lastMsg);


       }
   }
}
